<template>
  <div class="login">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      class="demo-ruleForm"
    >
      <!-- <el-form-item> -->
      <el-image
        src="http://120.53.31.103:84/uploads/image/2020-05-27/zmkrHVxaa4fVI5jtcNZ3xGy6KPU0QjXzxPjX7trh.jpeg"
      ></el-image>
      <!-- </el-form-item> -->
      <!-- <el-form-item> -->
      <p>欢迎使用后台管理系统</p>
      <!-- </el-form-item> -->
      <el-form-item prop="username">
        <el-input placeholder="请输入账号" v-model="ruleForm.username">
          <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="请输入密码"
          type="password"
          v-model="ruleForm.password"
        >
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let { data: res } = await this.$axios.login({
            mobile: this.ruleForm.username,
            password: this.ruleForm.password
          });
          console.log(res);
          if (res.code != 200) {
            this.$message.error(res.msg);
          } else {
            this.$message.success(res.msg);
            sessionStorage.setItem(
              "token",
              "Bearer " + res.data.remember_token
            );
            sessionStorage.setItem("actpath", "/index/User");
            this.$store.commit("getLoginUser", res.data);
            console.log(this.$store.state.loginUser);
            this.$router.push("/index");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../../public/scss/mixin.scss";
.login {
  width: 100%;
  height: 100%;
  background: url("http://120.53.31.103:84/adminstyle/img/login_bg.1de8de0c.png")
    no-repeat;
  background-size: 100% 100%;
}
.el-form {
  @include center();
  width: 368px;
  text-align: center;
  .el-button {
    width: 100%;
    margin-top: 30px;
  }
  p {
    font-size: 20px;
    margin-bottom: 20px;
    text-align: center;
    color: #2d8cf0;
    margin-top: 30px;
  }
  .el-image {
    width: 220px;
  }
}
</style>
